package com.sl.au.repository;


import com.sl.au.entity.Direction;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public interface DirectionRepository extends JpaRepository<Direction, String> {
    /**
     * 查询所有数据
     * @param key 查询条件
     */
    @Query(" select d from Direction d where d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' "
    		+ "or d.user.empLogin.userName like '%'+?1+'%'")
    Page<Direction> getName(String key,Pageable pageable);

    @Query(" select count(d) from Direction d where d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%'"
    		+ "or d.user.empLogin.userName like '%'+?1+'%'")
    Long getCount(String key);

    /**
     * 按部门筛选数据
     * @param key 查询条件
     * @param superior 部门
     * @param pageable
     * @return
     */
    @Query(" select d from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.saleArea.superior=?2")
    Page<Direction> getName(String key,String superior,Pageable pageable);

    @Query(" select count(d) from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.saleArea.superior=?2")
    Long getCount(String key,String superior);

    /**
     * 根据片区筛选数据
     * @param key 查询条件
     * @param areaId saleArea id
     * @param pageable
     * @return
     */
    @Query(" select d from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.saleArea.id=?2")
    Page<Direction> getNameBySaleArea(String key,String areaId,Pageable pageable);

    @Query(" select count(d) from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.saleArea.id=?2")
    Long getCountBySaleAreaId(String key,String areaId);

    /**
     * 根据本人筛选数据
     */
    @Query(" select d from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.id=?2")
    Page<Direction> getNameByUser(String key,String userId,Pageable pageable);

    @Query(" select count(d) from Direction d where (d.activityName like '%'+?1+'%' or d.address like '%'+?1+'%' or d.user.empLogin.userName like '%'+?1+'%') and d.user.id=?2")
    Long getCountByUser(String key,String userId);
}
